package com.shangcheng.server.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shangcheng.dubbo.api.UmsMenuApi;
import com.shangcheng.dubbo.api.UmsResourceApi;
import com.shangcheng.dubbo.api.UmsRoleApi;
import com.shangcheng.model.domain.*;
import com.shangcheng.model.vo.PageResult;
import com.shangcheng.server.exception.BusinessException;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

/**
 * @author shiqiang
 * @version 1.0
 * @date 2022/5/14 14:20
 */
@Service
public class UmsRoleService {
    @DubboReference
    private UmsMenuApi umsMenuApi;
    @DubboReference
    private UmsRoleApi umsRoleApi;
    @DubboReference
    private UmsResourceApi umsResourceApi;
    /**
     * 给角色分配菜单
     * @param menuIds 菜单ids
     * @param roleId  角色id
     */
    public void allocMenu(@RequestParam Integer[] menuIds, @RequestParam Integer roleId) {
        umsRoleApi.allocMenu(menuIds,roleId);
    }

    /**
     * 添加角色
     * @param umsRole
     */
    public void createRole(UmsRole umsRole) {
        UmsRole role= umsRoleApi.findRoleByAllName(umsRole);
        if (!ObjectUtils.isEmpty(role)){
            throw new BusinessException(role.getName()+"角色已存在");
        }
        umsRoleApi.createRole(umsRole);
    }

    /**
     * 批量删除角色
     * @param ids
     */
    public void deleteRole(Integer[] ids) {
        umsRoleApi.deleteRole(ids);
    }

    /**
     * 根据名称查询角色列表
     * @param keyword
     * @param pageNum
     * @param pageSize
     * @return
     */
    public PageResult findRoleByName(String keyword, Integer pageNum, Integer pageSize) {

        IPage<UmsRole> page = umsRoleApi.findRoleByName(keyword, pageNum, pageSize);
        return new PageResult(pageNum,pageSize, Convert.toInt(page.getTotal()),page.getRecords());
    }

    /**
     * 获取角色列表
     * @return
     */
    public List<UmsRole> findAllRole() {
        return umsRoleApi.findAllRole();
    }

    /**
     * 修改角色
     * @param umsRole
     */
    public void updateRole(UmsRole umsRole) {
        UmsRole role = umsRoleApi.findRoleByAllName(umsRole);
        if (!ObjectUtils.isEmpty(role)){
            if (!ObjectUtils.nullSafeEquals(umsRole.getId(),role.getId())){
                throw new BusinessException(role.getName()+"角色已存在");
            }
        }
        umsRoleApi.updateRole(umsRole);
    }

    /**
     * 修改角色状态
     * @param role
     */
    public void updateRoleStatus(UmsRole role) {
        umsRoleApi.updateRoleStatus(role);
    }

    /**
     * 获取角色相关菜单
     * @param roleId
     * @return
     */
    public List<UmsMenu> listMenu(Integer roleId) {
        //1.查询角色菜单关系表
        List<UmsRoleMenuRelation> list= umsRoleApi.findMenuIdByRole(roleId);
        //2.构建返回对象
        List<UmsMenu> menuList= new ArrayList<>();
        for (UmsRoleMenuRelation roleMenuRelation : list) {
            //获取菜单id
            Integer menuId = roleMenuRelation.getMenuId();
            UmsMenu menu = umsMenuApi.findById(menuId);
            menuList.add(menu);
        }
        return menuList;
    }

    /**
     * 获取角色相关资源
     * @param roleId
     * @return
     */
    public List<UmsResource> listResource(Integer roleId) {
        //1.查询角色资源关系表
        List<UmsRoleResourceRelation> list= umsRoleApi.findResourceIdByRole(roleId);
        //2.构建返回对象
        List<UmsResource> resourceList= new ArrayList<>();
        for (UmsRoleResourceRelation umsRoleResourceRelation : list) {
            //1.获取resourceId
            Integer resourceId = umsRoleResourceRelation.getResourceId();
            UmsResource resource = umsResourceApi.findUmsResourceById(resourceId);
            resourceList.add(resource);
        }
        return resourceList;
    }
}
